package com.l2jserver.gameserver.model.actor.status;

import java.text.DecimalFormat;

import com.l2jserver.gameserver.model.actor.FakePlayer;
import com.l2jserver.gameserver.model.actor.L2Attackable;
import com.l2jserver.gameserver.model.actor.L2Character;
import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
import com.l2jserver.gameserver.network.serverpackets.NicknameChanged;

public class AttackableStatus extends NpcStatus
{
	public AttackableStatus(L2Attackable activeChar)
	{
		super(activeChar);
	}
	
	@Override
	public final void reduceHp(double value, L2Character attacker)
	{
		reduceHp(value, attacker, true, false, false);
	}
	
	@Override
	public final void reduceHp(double value, L2Character attacker, boolean awake, boolean isDOT, boolean isHpConsumption)
	{
		if (getActiveChar().isDead())
		{
			return;
		}
		
		if (value > 0)
		{
			if (getActiveChar().isOverhit())
			{
				getActiveChar().setOverhitValues(attacker, value);
			}
			else
			{
				getActiveChar().overhitEnabled(false);
			}
		}
		else
		{
			getActiveChar().overhitEnabled(false);
		}
		
		super.reduceHp(value, attacker, awake, isDOT, isHpConsumption);
		
		if (!getActiveChar().isDead())
		{
			// And the attacker's hit didn't kill the mob, clear the over-hit flag
			getActiveChar().overhitEnabled(false);
		}
	}
	
	@Override
	public boolean setCurrentHp(double newHp, boolean broadcastPacket)
	{
		boolean result = super.setCurrentHp(newHp, broadcastPacket);
		
		L2Attackable activeChar = getActiveChar();
		
		// fissban - Mostramos la cant de HP q le queda al npc en el titulo solo si no es un bot
		
		FakePlayer fakePc = activeChar.getFake();
		if ((fakePc != null))
		{
			String title = fakePc.get_title();
			L2NpcTemplate npcTmpl = activeChar.getTemplate();
			if (title.isEmpty() && (npcTmpl.getTitle() != null))
			{
				title = npcTmpl.getTitle();
			}
			activeChar.setTitle(title + (title.isEmpty() ? "" : " - ") + "HP " + new DecimalFormat("#.##").format((100.0 * activeChar.getCurrentHp()) / activeChar.getMaxHp()) + "%");
			activeChar.broadcastPacket(new NicknameChanged(activeChar));
		}
		
		return result;
	}
	
	@Override
	public L2Attackable getActiveChar()
	{
		return (L2Attackable) super.getActiveChar();
	}
}